<?php
/**
 * Created by PhpStorm.
 * User: zhuyujun
 * Date: 2017/10/24
 * Time: 15:21
 */

namespace App\Api\Service\Order\Controllers;


use App\Eloquent\Ygt\Order;
use App\Eloquent\Ygt\OrderListRelation;
use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Privilege;
use App\Engine\OrderType;
use App\Engine\Func;


use Framework\Services\ImageUpload\imageProcess;


class OrderController extends Controller
{
    //参考 IndexController/getOrderList方法
    //原有IndexController文件太大,编写太卡
    public function historyList()
    {
        $userId             = Func::getHeaderValueByName('userid',25);
        $loginUser          = DepartmentUser::getCurrentInfo($userId);
        $customerOrderId    = request('customer_order_id',0);//订单号
        if(!$loginUser)
        {
            xThrow(ERR_PARAMETER,'登录超时');
        }
        //判断用户是否为管理员
        $userInfo       = $loginUser->toArray();
        $companyId      = $userInfo['company_id'];
        //工单筛选
        $orderStautsArr = [
            1=>[1,101],
            2=>[2],
            3=>[3],
            4=>[4],
        ];
        $orderStauts    = request('order_status',0);//筛选的工单状态，1-待开工 2-进行中 3-已完成,4-待派发
        $orderTitle     = request('order_title','');//筛选的工单号
        $stautsArr      = isset($orderStautsArr[$orderStauts]) ? $orderStautsArr[$orderStauts] : [];

        $selectSql      = 'a.*,b.status as is_distribute,b.order_type';
        $query          = \DB::table('ygt_order_list_relation as a');
        $query          = $query->leftJoin('ygt_order as b','b.id','=','a.relate_id');
        $query          = $query->selectRaw($selectSql);
        $query          = $query->where('a.type',1);
        if(!empty($stautsArr))
        {
            $query      = $query->whereIn('a.status',$stautsArr);
        }
        if(!empty($orderTitle))
        {
            $query      = $query->where('a.order_title','like','%'.$orderTitle.'%');
        }
        $query          = $query->where('b.customer_order_id',$customerOrderId);
        $orderCollection= $query->orderBy('updated_at','desc')->get();
        $orderList      = [];
        foreach($orderCollection as $key=>$val)
        {
            $arr            = (array)$val;
            $orderList[]    = $arr;
        }
        return $this->distributeData($orderList);

    }
    //获取工单列表筛选状态列表
    public function historyFilterStatus()
    {
        $result             = [
            [
                'title' => '全部',
                'value' => '',
            ],
            [
                'title' => '待开工',
                'value' => '1',
            ],
            [
                'title' => '进行中',
                'value' => '2',
            ],
            [
                'title' => '已完成',
                'value' => '3',
            ],
            [
                'title' => '待派发',
                'value' => '4',
            ]
        ];
        return $result;
    }
    private function distributeData($orderList)
    {

        $returnOrderList = [];
        $dataList = [];
        foreach ($orderList as $orderRow) {
            $type = $orderRow['type'];//工单类型 1-主工单 2-工序管理员工单 3-查看权限用户的工序工单 4-员工工单
            $status = $orderRow['status'];//不同类型工单，状态有所不同
            $orderRowContent = unserialize($orderRow['content']);

            //获取生产实例图
//            }
            $productionCaseDiagram = \App\Engine\OrderEngine::getOrderFiledValueTrue($orderRowContent['production_case_diagram'], 9);


            //处理字段
            $orderRowContent = \App\Engine\OrderEngine::dealOrderFiled($orderRowContent);

            //订单创建时间（派发时间）
            $orderCreateTime = $orderRow['order_create_time'];
            if ($orderCreateTime) {
                $orderCreateTime = date('Y-m-d', $orderCreateTime);
            }

            //获取工序类型名称
            $orderTypeTitle = OrderType::getOneValueById($orderRowContent['order_type'], 'title');

            //新增相关图片的ID
            $imgId = $orderRowContent['production_case_diagram'];


            //工单列表不同类型相同字段，统一管理
            $productName = \App\Engine\OrderEngine::getOrderFiledValueTrue($orderRowContent['product_name'], 20);
            $commonArr = [
                '$customer_title' => '单位名称',//工单列表展示字段
                '$product_title' => '品名',//工单列表展示字段
                '$order_title' => '单号',//工单列表展示字段
                '$customer_name' => isset($orderRowContent['field_name_23']) ? $orderRowContent['field_name_23'] : '',//新加字段客户
                '$product_name' => isset($productName) ? $productName : '',//新加字段品名
                '$order_title_value' => $orderRowContent['order_title'],//新加字段单号
                '$firstPropertyName' => '成品规格',
                '$secondPropertyName' => '数量',
                '$thirdPropertyName' => '交货日期',
                '$thirdPropertyValue' => $orderRowContent['finished_date'],
                '$secondPropertyValue' => $orderRowContent['product_num'],
                '$firstPropertyValue' => isset($orderRowContent['finished_specification']) ? $orderRowContent['finished_specification'] : '',
                '$productionCaseDiagram' => $productionCaseDiagram,
                '$productOrderTimeValue' => $orderCreateTime,
                '$orderTypeTitle' => $orderTypeTitle,
                '$orderNumberValue' => $orderTypeTitle,
                '$orderTimeValue' => $orderRowContent['finished_date'],
                '$relate_id' => $orderRow['relate_id'],
                'is_distribute' => $orderRow['is_distribute'],
                'order_type' => $orderRow['order_type'],
                'img_id' => $imgId,
            ];

            if ($type == 1) {//1-主工单
                if ($status == 1) {//待接单
                    $tmpArr = [
                        'idName' => 5,
                        '$itemFunctionName' => 'mangerDoneCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待接单',
                        '$statusColor' => 'FFB401',
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);

                } elseif ($status == 101) {//待开工
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'mangerDoneCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待开工',
                        '$statusColor' => 'FFB401',
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 2) {//生产中
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'mangerDoneCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '生产中',
                        '$statusColor' => 'FE7E57',
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 3) {//已完工
                    $tmpArr = [
                        'idName' => 6,
                        '$itemFunctionName' => 'mangerDoneCellFunction' . $orderRow['relate_id'],
                        '$orderSureName' => 'orderSure' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '已完成',
                        '$statusColor' => '04C9B3',
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 4) {//待派发
                    //continue;//未派发工单另外处理
                    $tmpArr = [
                        'idName' => 7,
                        '$itemFunctionName' => 'itemFunctionName' . $orderRow['relate_id'],
                        '$orderSureName' => 'orderSureName'.$orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待派发',
                        '$statusColor' => 'B5B5B5',
                    ];


                    /**只有下单人有权限派发工单**/
                    //获取登陆用户信息
                    $userId = \App\Engine\Func::getHeaderValueByName('userid');
                    $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();
                    //获取某权限的用户
                    $companyId = $orderRowContent['company_id'];
                    $privilegeIdList = \App\Engine\OrderEngine::getPrivilegeByNode($companyId, 1);
                    if (!in_array($userInfo['privilege_id'], $privilegeIdList)) {
                        $tmpArr[ 'is_distribute'] = 0;
                    }

                    $dataList[] = array_merge($commonArr, $tmpArr);
                }
            } elseif ($type == 2) {//2-工序管理员工单
                if ($status == 1) {//待接单
                    $tmpArr = [
                        'idName' => 1,
                        '$itemFunctionName' => 'productdirectorWaitCellFunction' . $orderRow['relate_id'],
                        '$orderSureName' => 'productdirectorOrderSure' . $orderRow['relate_id'],
                        '$orderSureBgImage' => 'orderCellStatusButton.png',
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待接单',
                        '$statusColor' => 'FFB401',
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 2) {//生产中-未分配工单
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'productdirectorWaitCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待分配',
                        '$statusColor' => 'FFB401',//待接单,待分派,待开工
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 101) {//待开工
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'productdirectorproductIngCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待开工',
                        '$statusColor' => 'FFB401',//待接单,待分派,待开工
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 3) {//生产中-已分配工单
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'productdirectorproductIngCellFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '生产中',
                        '$statusColor' => 'FE7E57',//生产中
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);
                } elseif ($status == 4) {//已完工
                    $tmpArr = [
                        'idName' => 4,
                        '$itemFunctionName' => 'productdirectorDoneCellFunction' . $orderRow['relate_id'],
                        '$orderSureName' => 'orderSure' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '已完成',
                        '$statusColor' => '04C9B3',//已完成
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);;
                }
            } elseif ($type == 3) {//3-查看权限用户的工序工单
                $orderStatusArr = [
                    '1' => '待接单',
                    '2' => '待分派',
                    '3' => '生产中',
                    '4' => '已完成',
                    '101' => '待开工',
                ];
                $orderStatusColorArr = [
                    '1' => 'FFB401',
                    '2' => 'FFB401',
                    '3' => 'FE7E57',
                    '4' => '04C9B3',
                    '101' => 'FFB401',
                ];

                if (!isset($orderStatusArr[$status])) {//异常处理
                    $orderStatusArr[$status] = '异常状态';
                    $orderStatusColorArr[$status] = 'FFB401';
                }

                $tmpArr = [
                    'idName' => 4,
                    '$itemFunctionName' => 'productdirectorDoneCellFunction' . $orderRow['relate_id'],
                    '$orderSureName' => 'orderSure' . $orderRow['relate_id'],
                    '$statusCode' => $status,//新增订单状态值
                    '$orderStatus' => $orderStatusArr[$status],
                    '$statusColor' => $orderStatusColorArr[$status],//待接单,待分派,待开工
                ];
                $dataList[] = array_merge($commonArr, $tmpArr);;
            } elseif ($type == 4) {//4-员工工单

                if ($status == 1) {//待接单
                    $tmpArr = [
                        'idName' => 1,
                        '$itemFunctionName' => 'waitCellFunction' . $orderRow['relate_id'],
                        '$orderSureName' => 'orderSure' . $orderRow['relate_id'],
                        '$orderSureBgImage' => 'draftsOrderCellStatusButton.png',
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待开工',
                        '$statusColor' => 'FFB401',//待接单,待分派,待开工
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);;
                } elseif ($status == 2) {//生产中-未领取材料
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'productIngNoneGetFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '待领料',
                        '$statusColor' => 'FFB401',//待接单,待分派,待开工
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);;
                } elseif ($status == 3) {//生产中-已领取材料
                    $tmpArr = [
                        'idName' => 3,
                        '$itemFunctionName' => 'productIngNoneDoneFunction' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '生产中',
                        '$statusColor' => 'FE7E57',//生产中
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);;
                } elseif ($status == 4) {//已完工
                    $tmpArr = [
                        'idName' => 4,
                        '$itemFunctionName' => 'doneCellFunction' . $orderRow['relate_id'],
                        '$orderSureName' => 'orderSure' . $orderRow['relate_id'],
                        '$statusCode' => $status,//新增订单状态值
                        '$orderStatus' => '已完成',
                        '$statusColor' => '04C9B3',//已完成
                    ];
                    $dataList[] = array_merge($commonArr, $tmpArr);;
                }
            }
        }

        //如果是客户账号，不进入工单详情
        //获取登陆用户信息
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();
        if($userInfo['privilege_id'] == 110){
            foreach ($dataList as $tmpKey => $tmpRow){
                $dataList[$tmpKey]['$itemFunctionName'] = '';
                $dataList[$tmpKey]['is_distribute'] = 0;
            }
        }

        $returnOrderList['pageID'] = 0;
        $returnOrderList['data']['$tableviewSource'] = $dataList;
        return $returnOrderList;
    }

    //待入库列表
    public function getWaitWarehousingList()
    {
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();
        $companyId = $userInfo['company_id'];

        //品名、工单号、数量、工序名称、示例图
        $page               = request( 'page', 1 );
        $limit              = request('limit', 10 );
        $offset             = ($page - 1) * $limit;
        $offset = ($page - 1) * $limit;
        $where = ['company_id'=>$companyId,'status'=>1];
        $count = \App\Eloquent\Ygt\OrderFinishedProduct::where($where)->count();
        $tmpOrderFinishedProductList = \App\Eloquent\Ygt\OrderFinishedProduct::where($where)->skip($offset)->take($limit)->orderBy('id','desc')->get();

        $returnOrderFinishedProductList = [];
        foreach ($tmpOrderFinishedProductList as $tmpOrderFinishedProductRow){
            //工艺名称，工单号，数量，规格
            $orderId = $tmpOrderFinishedProductRow['order_id'];
            $orderRow = \App\Eloquent\Ygt\Order::where(['id'=>$orderId])->first();

            $orderTypeTitle        = \App\Engine\OrderType::getOneValueById($orderRow['order_type'], 'title');//工艺名称

//            //成品规格数据处理
//            if (isset($orderRow['finished_specification'])) {
//                $tmpArr = explode(',', $orderRow['finished_specification']);
//                if (count($tmpArr) == 2) {
//                    $orderRow['finished_specification'] = sprintf("%s×%s厘米", $tmpArr[0], $tmpArr[1]);
//                }
//
//                if (count($tmpArr) == 3) {
//                    $orderRow['finished_specification'] = sprintf("%s×%s×%s厘米", $tmpArr[0], $tmpArr[1], $tmpArr[2]);
//                }
//
//                //如果数据都为空，显示为空
//                $hasData = false;
//                foreach ($tmpArr as $tmpValue) {
//                    if ($tmpValue) {
//                        $hasData = true;
//                    }
//                }
//
//                if (!$hasData) {
//                    $orderRow['finished_specification'] = '';
//                }
//            }

            $orderRow['order_title'] = \App\Engine\Common::changeSnCode($orderRow['order_title']);

            $customFieldsText = '';
            if($orderRow['finished_weight'] && $orderRow['product_num']){
                $weightAll = $orderRow['finished_weight'] * $orderRow['product_num'];
                if($weightAll){
                    $customFieldsText = "重量：{$weightAll}".$orderRow['unit'];
                }
            }


            //获取工单的示例图
            //获取生产实例图
            $productionCaseDiagram = \App\Engine\OrderEngine::getOrderFiledValueTrue($orderRow['production_case_diagram'], 9);

            $productInfo = \App\Eloquent\Ygt\BuyersProduct::where('id','=',$orderRow['product_name'])->first();
            $product_name = '';
            if($productInfo){
                $productInfo = $productInfo->toArray();
                $product_name = $productInfo['name'];
            }

            $orderRowUnit = '';
            if($orderRow['product_num']){
                $tmpArr = explode(',',$orderRow['product_num']);
                if(count($tmpArr) == 2 && $tmpArr[1]){
                    $orderRowUnit = $tmpArr[1];
                }
            }

            $returnOrderFinishedProductList[] = [
                'id' => $tmpOrderFinishedProductRow['id'],
                'order_title' => $orderRow['order_title'],
                'order_type_title' => $orderTypeTitle,
                'number' => $tmpOrderFinishedProductRow['number'].$orderRowUnit,
                'product_name'=>$product_name,
                'production_case_diagram' => $productionCaseDiagram,
            ];
        }

        $pagingData = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($returnOrderFinishedProductList, $pagingData);

    }

    //半成品待打印入口
    public function getWaitPrintOrderProcessProduct()
    {
        //获取登陆用户信息
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();

        $companyId = $userInfo['company_id'];

        //获取待打印列表
        $tmpOrderProcessProductList = \App\Eloquent\Ygt\OrderProcessProduct::where(['company_id'=>$companyId,'status'=>1])->get();


        $reList = [];
        foreach ($tmpOrderProcessProductList as $tmpOrderProcessProductRow){
            $tmpOrderId = $tmpOrderProcessProductRow['order_id'];
            $tmpOrderProcessId = $tmpOrderProcessProductRow['order_process_id'];

            $tmpOrderRow = \App\Eloquent\Ygt\Order::where(['id'=>$tmpOrderId])->first();
            $tmpOrderTitle = $processTitle = $productName = $operatorName = '';
            if($tmpOrderRow){
                //工单号
                $tmpOrderTitle = \App\Engine\Common::changeSnCode($tmpOrderRow['order_title']);

                //工序名称
                $buyersProduct = \App\Eloquent\Ygt\BuyersProduct::where(['id' => $tmpOrderRow['product_name']])->first();
                if ($buyersProduct) {
                    $productName = $buyersProduct['name'];
                }

                //工序名称
                $tmpOrderProcessRow = \App\Eloquent\Ygt\OrderProcess::where(['id'=>$tmpOrderProcessId])->first();
                $processTitle = \App\Eloquent\Ygt\Process::getOneValueById($tmpOrderProcessRow['process_type'], 'title');
                $processTitle .= "半成品";
            }

            //操作员
            $tmpUid = $tmpOrderProcessProductRow['uid'];
            $tmpUserInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($tmpUid);
            if($tmpUserInfo){
                $operatorName = $tmpUserInfo['truename'];
            }

            $reList[] = [
                'order_process_product_id' => $tmpOrderProcessProductRow['id'],//半成品ID
                'process_title' => $processTitle,//工序名称
                'order_title' => $tmpOrderTitle,//工单号
                'product_name' => $productName,//品名
                'number' => $tmpOrderProcessProductRow['number'],//数量
                'operator_name' => $operatorName,//操作员
                'submit_time' => date('Y-m-d H:i:s',($tmpOrderProcessProductRow->toArray())['created_at']),//提交时间
            ];
        }

        return $reList;
    }

    //半成品待打印入口
    public function printOrderProcessProduct()
    {
        //处理传入json数据
        $orderPorcessProductListJson = request('order_process_product_list');
        $orderProcessProductList = json_decode(htmlspecialchars_decode($orderPorcessProductListJson), true);

        //检验数量是否符合要求
        foreach ($orderProcessProductList as $orderProcessProductRow){
            $tmpOrderProcessProudctId = $orderProcessProductRow['order_process_product_id'];
            $tmpOrderProcessProductRow = \App\Eloquent\Ygt\OrderProcessProduct::where(['id'=>$tmpOrderProcessProudctId])->first();
            $postAllNum = 0;
            foreach ($orderProcessProductRow['num_list'] as $tmpPostNum){
                $postAllNum += $tmpPostNum;
            }

            if($tmpOrderProcessProductRow){
                if(round($tmpOrderProcessProductRow['number']) > round($postAllNum)){
                    return [
                        'code' => 1,
                        'message' => '修改后的数量不能小于原半成品数量'
                    ];
                }
                if(round($tmpOrderProcessProductRow['number']) < round($postAllNum)){
                    return [
                        'code' => 1,
                        'message' => '修改后的数量不能大于原半成品数量'
                    ];
                }
            }

            if($tmpOrderProcessProductRow['status'] == 2){
                return [
                    'code' => 1,
                    'message' => '已打印，如需重新打印请去打印记录打印'
                ];
            }

        }

        //生成打码所需要的数据
        $resultData = [];
        foreach ($orderProcessProductList as $orderProcessProductRow) {
            $tmpOrderProcessProudctId = $orderProcessProductRow['order_process_product_id'];
            $tmpOrderProcessProductRow = \App\Eloquent\Ygt\OrderProcessProduct::where(['id' => $tmpOrderProcessProudctId])->first();

            $tmpOrderId = $tmpOrderProcessProductRow['order_id'];
            $tmpOrderProcessId = $tmpOrderProcessProductRow['order_process_id'];

            $tmpOrderRow = \App\Eloquent\Ygt\Order::where(['id'=>$tmpOrderId])->first();
            $tmpOrderTitle = $processTitle = $productName = $operatorName = '';
            if($tmpOrderRow) {
                //工单号
                $tmpOrderTitle = \App\Engine\Common::changeSnCode($tmpOrderRow['order_title']);

                //工序名称
                $buyersProduct = \App\Eloquent\Ygt\BuyersProduct::where(['id' => $tmpOrderRow['product_name']])->first();
                if ($buyersProduct) {
                    $productName = $buyersProduct['name'];
                }

                //工序名称
                $tmpOrderProcessRow = \App\Eloquent\Ygt\OrderProcess::where(['id' => $tmpOrderProcessId])->first();
                $processTitle = \App\Eloquent\Ygt\Process::getOneValueById($tmpOrderProcessRow['process_type'], 'title');
                $processTitle .= "半成品";
            }

            if($tmpOrderProcessProductRow){
                foreach ($orderProcessProductRow['num_list'] as $tmpPostNum){

                    //添加到二维码通用表
                    $qrcodeSn = \App\Eloquent\Ygt\Qrcode::getSn($tmpOrderProcessProudctId);

                    $tmpObj = \App\Eloquent\Ygt\QrcodeCommon::firstOrNew(['id'=>'']);
                    $tmpInsertRow = [
                        'sn' => $qrcodeSn,
                        'relate_id' => $tmpOrderProcessProudctId,
                        'relate_type' => 1,
                        'total_number' => $tmpPostNum,
                        'now_number' => $tmpPostNum,
                        'company_id' => $tmpOrderProcessProductRow['company_id'],
                    ];

                    $tmpObj->fill($tmpInsertRow);
                    $tmpObj->save();
                    $qrcodeId = $tmpObj->id;

                    $qrcodeStr          = "1-{$qrcodeId}-{$qrcodeSn}";
                    $printData[] = \App\Engine\KmPrinter::text($processTitle, 10, 15, 4);//工序名词
                    $printData[] = \App\Engine\KmPrinter::line(10, 55, 590, 55);
                    $printData[] = \App\Engine\KmPrinter::text($tmpOrderTitle, 10, 95, 3);//工单号
                    $printData[] = \App\Engine\KmPrinter::text($productName, 10, 135, 3);//品名
                    $printData[] = \App\Engine\KmPrinter::text('数量：'.$tmpPostNum, 10, 175, 3);//数量

                    $fieldsList = \App\Eloquent\Ygt\ProcessProductCodeSet::getList(['process_product_id'=>['=',$tmpOrderProcessProudctId]], '', 6, '', ['id', 'asc']);
                    $i = 1;
                    foreach ($fieldsList as $key => $val) {
                        $n = $key % 2;
                        $y = 175 + $i * 40;
                        if ($n == 1) {
                            $x = 200;
                            $i += 1;
                        } else {
                            $x = 10;
                        }
                        $text = $val['content'];
                        $printData[] = \App\Engine\KmPrinter::text($text, $x, $y, 5);
                    }

                    $printData[] = \App\Engine\KmPrinter::qrcode($qrcodeStr, 440, 80);
                    $printResult = \App\Engine\KmPrinter::output($printData);
                    $result[] = [
                        'pinfo' => $printResult
                    ];
                }
            }

            //修改待打印半成品表状态
            $tmpOrderProcessProductRow = \App\Eloquent\Ygt\OrderProcessProduct::where(['id'=>$tmpOrderProcessProudctId])->first();
            $tmpOrderProcessProductRow->status = 2;
            $tmpOrderProcessProductRow->save();
        }

        return $result;
    }

    //半成品打印记录入口
    public function getOrderProcessProductPrintHistory(){
        //获取登陆用户信息
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();
        $companyId = $userInfo['company_id'];

        $tmpQrcodeCommonList = \App\Eloquent\Ygt\QrcodeCommon::where(['company_id'=>$companyId,'relate_type'=>1])->orderBy('id','desc')->get();
        $reList = [];
        foreach ($tmpQrcodeCommonList as $tmpQrcodeCommonRow){
            $tmpOrderProcessProductId = $tmpQrcodeCommonRow['relate_id'];
            $tmpOrderProcessProductRow = \App\Eloquent\Ygt\OrderProcessProduct::where(['id'=>$tmpOrderProcessProductId])->first();

            $tmpOrderId = $tmpOrderProcessProductRow['order_id'];
            $tmpOrderProcessId = $tmpOrderProcessProductRow['order_process_id'];

            $tmpOrderRow = \App\Eloquent\Ygt\Order::where(['id'=>$tmpOrderId])->first();
            $tmpOrderTitle = $processTitle = $productName = $operatorName = '';
            if($tmpOrderRow){
                //工单号
                $tmpOrderTitle = \App\Engine\Common::changeSnCode($tmpOrderRow['order_title']);

                //工序名称
                $buyersProduct = \App\Eloquent\Ygt\BuyersProduct::where(['id' => $tmpOrderRow['product_name']])->first();
                if ($buyersProduct) {
                    $productName = $buyersProduct['name'];
                }

                //工序名称
                $tmpOrderProcessRow = \App\Eloquent\Ygt\OrderProcess::where(['id'=>$tmpOrderProcessId])->first();
                $processTitle = \App\Eloquent\Ygt\Process::getOneValueById($tmpOrderProcessRow['process_type'], 'title');
                $processTitle .= "半成品";
            }

            //操作员
            $tmpUid = $tmpOrderProcessProductRow['uid'];
            $tmpUserInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($tmpUid);
            if($tmpUserInfo){
                $operatorName = $tmpUserInfo['truename'];
            }

            //增加二维码信息
            $printData = [];
            $printData[] = \App\Engine\KmPrinter::text($processTitle, 10, 15, 4);//工序名词
            $printData[] = \App\Engine\KmPrinter::line(10, 55, 590, 55);
            $printData[] = \App\Engine\KmPrinter::text($tmpOrderTitle, 10, 95, 3);//工单号
            $printData[] = \App\Engine\KmPrinter::text($productName, 10, 135, 3);//品名
            $printData[] = \App\Engine\KmPrinter::text('数量：'.$tmpQrcodeCommonRow['total_number'], 10, 175, 3);//数量

            $fieldsList = \App\Eloquent\Ygt\ProcessProductCodeSet::getList(['process_product_id'=>['=',$tmpOrderProcessProductRow['process_product_id']]], '', 6, '', ['id', 'asc']);
            $i = 1;
            foreach ($fieldsList as $key => $val) {
                $n = $key % 2;
                $y = 175 + $i * 40;
                if ($n == 1) {
                    $x = 200;
                    $i += 1;
                } else {
                    $x = 10;
                }
                $text = $val['content'];
                $printData[] = \App\Engine\KmPrinter::text($text, $x, $y, 5);
            }



            $qrcodeStr          = "1-{$tmpQrcodeCommonRow['id']}-{$tmpQrcodeCommonRow['sn']}";
            $printData[] = \App\Engine\KmPrinter::qrcode($qrcodeStr, 440, 80);
            $printResult = \App\Engine\KmPrinter::output($printData);

            $reList[] = [
                'order_process_product_id' => $tmpOrderProcessProductRow['id'],//半成品ID
                'process_title' => $processTitle,//工序名称
                'order_title' => $tmpOrderTitle,//工单号
                'product_name' => $productName,//品名
                'number' => $tmpQrcodeCommonRow['total_number'],//数量
                'operator_name' => $operatorName,//操作员
                'submit_time' => date('Y-m-d H:i:s',($tmpOrderProcessProductRow->toArray())['updated_at']),//提交时间
                'pinfo' => $printResult,//提交时间
            ];

        }

        return $reList;

    }


}